package br.com.sigad.servico;

import java.sql.Connection;
import java.util.Collection;
import java.util.List;

import br.com.sigad.dao.ConnectionFactory;
import br.com.sigad.dao.DependenteDAO;
import br.com.sigad.dto.DependenteDTO;

public class DependenteService extends Service{

	public void gravarDep(DependenteDTO dep) throws Exception {
		Connection conn = null;
		try{
			conn = ConnectionFactory.getConnection();
			DependenteDAO dao = new DependenteDAO();
			dao.gravar(dep,conn);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			fecharConexao(conn);
		}
	}

	public void alterarDep(DependenteDTO dep) throws Exception {
		Connection conn = null;
		try{
			conn = ConnectionFactory.getConnection();
			DependenteDAO dao = new DependenteDAO();
			dao.alterar(dep,conn);
			conn.commit();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			fecharConexao(conn);
		}
	}

	public void excluirDep(DependenteDTO dep) throws Exception {
		Connection conn = null;
		try{
			conn = ConnectionFactory.getConnection();
			DependenteDAO dao = new DependenteDAO();
			dao.excluir(dep,conn);
			conn.commit();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			fecharConexao(conn);
		}
	}
	
	public Collection<DependenteDTO> pesquisarDep(DependenteDTO dep) throws Exception{
		Connection conn = null;
		try{
			conn = ConnectionFactory.getConnection();
			DependenteDAO dao = new DependenteDAO();
			return dao.pesquisar(dep, conn);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			fecharConexao(conn);
		}
	}

	public List<DependenteDTO> pesquisaDadosDependentes(DependenteDTO dep)throws Exception {
		Connection conn = null;
		try{
			conn = ConnectionFactory.getConnection();
			DependenteDAO dao = new DependenteDAO();
			return dao.pesquisarDependentes(dep, conn);
		}finally{
			fecharConexao(conn);
		}
	}
}